package com.qk.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Course;
import com.qk.mapper.CourseMapper;
import com.qk.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Slf4j
@Service
public class CourseServiceImpl implements CourseService {

    @Autowired
    private CourseMapper courseMapper;
    /**
     * 添加课程
     */
    @Override
    public void add(Course course) {
        //补全基础信息
        course.setCreateTime(LocalDateTime.now());
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.insert(course);
    }


    /**
     * 获取课程列表
     */
    @Override
    public PageResult<Course> listCourse(String name, Integer subject, Integer target, Integer page, Integer pageSize) {

        PageHelper.startPage(page,pageSize);

        List<Course> list = courseMapper.selectList(name, subject, target);

        Page<Course> p = (Page<Course>) list;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 查询所有课程
     */
    @Override
    public PageResult<Course> list() {
        PageHelper.startPage(1,10);
        List<Course> list = courseMapper.list();
        Page<Course> p = (Page<Course>) list;
        return new PageResult<>(p.getTotal(),p.getResult());
    }

    /**
     * 根据ID查询课程回显
     */
    @Override
    public Course getById(Integer id) {
        return courseMapper.getById(id);
    }

    /**
     * 修改课程
     */
    @Override
    public void update(Course course) {
        //补全基础信息
        course.setUpdateTime(LocalDateTime.now());
        courseMapper.update(course);
    }

    /**
     * 删除课程
     */
    @Override
    public void deleteById(Integer id) {
        courseMapper.deleteById(id);
    }

    /**
     * 根据学科查询课程
     */
    @Override
    public List<Course> listSubject(Integer subject) {
        PageHelper.startPage(1,10);
        return courseMapper.listSubject(subject);
    }
}
